﻿namespace com.oakwoodsys.ConsultantCare
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using com.oakwoodsys.ConsultantCareDao;
    using com.oakwoodsys.Extensions.SharePointExtensions;
    using com.oakwoodsys.SharePoint;

    using Microsoft.SharePoint;

    using ReferenceDao = ConsultantCareDao.ConsultantDao;

    using ReferenceObject = ConsultantCareDao.Consultant;

    public class Consultant_Definition_WebLevel : CustomBaseDefinition
    {
        #region Constructors

        public Consultant_Definition_WebLevel( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~Consultant_Definition_WebLevel( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateList( getListDefn( ) );
            addEventReceiver( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
        }

        void addEventReceiver( )
        {
            string assembly = Assembly.GetAssembly( typeof( ConsultantItemEventReceiver ) ).FullName;
            string className = typeof( ConsultantItemEventReceiver ).FullName;

            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( ReferenceDao.ListName );

                List<SPEventReceiverDefinition> l = new List<SPEventReceiverDefinition>( );
                foreach ( SPEventReceiverDefinition v in spList.EventReceivers )
                    if ( ( v.Type == SPEventReceiverType.ItemAdded ||
                        v.Type == SPEventReceiverType.ItemAdding ||
                        v.Type == SPEventReceiverType.ItemDeleting ||
                        v.Type == SPEventReceiverType.ItemUpdated ||
                        v.Type == SPEventReceiverType.ItemUpdating ) &&
                        v.Assembly.Equals( assembly ) && v.Class.Equals( className ) )
                        l.Add( v );
                for ( int i = l.Count - 1; i >= 0; i-- )
                    l[ i ].Delete( );

                spList.EventReceivers.Add( SPEventReceiverType.ItemAdded, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemAdding, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemDeleting, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemUpdated, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemUpdating, assembly, className );
            }
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = ReferenceDao.ListName,
                Title = "Consultants",
                Description = "Contains Consultant Care Consultants",
                ContentTypes = new string[ ]
                {
                    Item.GetContentTypeName<ReferenceObject>( )
                }.ToList( )
            };
        }

        #endregion Methods
    }
}